#if 0
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void sigint_handler(int sig){
    printf("替换 SIGINT 信号成功!!\n");
    exit(EXIT_SUCCESS);
}

int main(){
    if(signal(SIGINT, sigint_handler) == SIG_ERR){//将SIGINT信号与自定义函数关联起来，若返回SIG_ERR，则关联失败
        perror("signal error!!");
        exit(EXIT_FAILURE);
    }
    printf("Waiting for SIGINT...\n");
    while(1){
        // Do nothing
    }
    return 0;
}

#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
    __sigset_t set;
    // 获取当前进程的信号掩码
    if (sigprocmask(0, NULL, &set) < 0) {
        perror("sigprocmask error!!");
        return -1;
    }

    // 检查是否阻塞了 SIGINT 信号
    int is_blocked = sigismember(&set, SIGINT);//该进程的阻塞信号集中是否包含SIGINT信号
    if (is_blocked) {
        printf("SIGINT is blocked.\n");
    } else {
        printf("SIGINT is not blocked.\n");
    }

    return 0;
}


#include <stdio.h>
#include <pthread.h>

int count = 0;
pthread_mutex_t mutex;//定义一个互斥锁的操作句柄
void *thread_func(void *arg) {
    for (int i = 0; i < 1000; i++) {
        pthread_mutex_lock(&mutex);//加锁操作
        count++;
        pthread_mutex_unlock(&mutex);//解锁操作
    }
    return NULL;
}

int main() {
    pthread_t t1, t2;
    pthread_mutex_init(&mutex, NULL);//创建互斥锁

    int ret1=pthread_create(&t1, NULL, &thread_func, NULL);
    int ret2=pthread_create(&t2, NULL, &thread_func, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    pthread_mutex_destroy(&mutex);
    printf("count = %d\n", count);
    return 0;
}

#endif

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int count = 0;
pthread_mutex_t mutex;//定义一个互斥锁的操作句柄
void* thread_func(void* arg)
{
    pthread_mutex_lock(&mutex);
    count++;
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

int main() 
{
    pthread_t tid[5];
    pthread_mutex_init(&mutex, NULL);//初始化互斥锁

    for(int i=0;i<5;i++)//多线程访问共享资源
    {
        pthread_create(&tid[i],NULL,thread_func,NULL);
    }

    for(int i=0;i<5;i++)//等待多线程访问结束
    {
        pthread_join(tid[i],NULL);
    }
    pthread_mutex_destroy(&mutex);//销毁互斥锁
    printf("count = %d\n", count);
    return 0;
}